package cn.iocoder.yudao.module.reading.controller.app.community;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.draft.DraftCreateReqVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.draft.DraftRespVO;
import cn.iocoder.yudao.module.reading.service.community.CommunityDraftService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 用户 APP - 社区草稿 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 社区草稿")
@RestController
@RequestMapping("/reading/community/drafts")
@Validated
@Slf4j
public class CommunityDraftController {

    @Resource
    private CommunityDraftService draftService;

    @PostMapping("/save")
    @Operation(summary = "保存草稿")
    @PreAuthenticated
    public CommonResult<Long> saveDraft(@Valid @RequestBody DraftCreateReqVO createReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        Long draftId = draftService.saveDraft(createReqVO, userId);
        return success(draftId);
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新草稿")
    @PreAuthenticated
    public CommonResult<DraftRespVO> getLatestDraft(
            @Parameter(description = "草稿类型", required = true) @RequestParam("type") String type) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        DraftRespVO draft = draftService.getLatestDraft(userId, type);
        return success(draft);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除草稿（逻辑删除）")
    @PreAuthenticated
    public CommonResult<Boolean> deleteDraft(
            @Parameter(description = "草稿类型", required = true) @RequestParam("type") String type) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        draftService.deleteDraft(userId, type);
        return success(true);
    }

    @DeleteMapping("/physical-delete")
    @Operation(summary = "物理删除草稿", description = "用于发布动态后彻底删除草稿数据")
    @PreAuthenticated
    public CommonResult<Boolean> physicalDeleteDraft(
            @Parameter(description = "草稿类型", required = true) @RequestParam("type") String type) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        draftService.physicalDeleteDraft(userId, type);
        return success(true);
    }

}
